package ru.evga.inse.server.gwt.admin.client;

import ru.evga.gwt.lib.common.client.place.PlaceImpl;
import ru.evga.inse.server.gwt.admin.client.menu.AdminMenuItem;
import ru.evga.inse.server.gwt.admin.client.place.AdminClientPlaceHistoryMapper;
import ru.evga.inse.server.gwt.admin.client.place.AdminMenuPlaceUtil;
import ru.evga.inse.server.gwt.common.client.AbstractApp;

import com.google.gwt.activity.shared.ActivityManager;
import com.google.gwt.core.client.GWT;
import com.google.gwt.place.shared.PlaceHistoryHandler;


/**
 * Application for browsing entities.
 */
public class AdminApp
    extends AbstractApp
{
    private IAdminAppFactory factory;
    
    private static AdminApp INSTANCE;

    static public AdminApp getApplicationInstance()
    {
        if(INSTANCE == null)
        {
            IAdminAppFactory factory = GWT.create(AdminAppFactoryImpl.class); 
            INSTANCE = new AdminApp(factory);
        }
        
        return INSTANCE;
    }
    
    private AdminApp(IAdminAppFactory factory)
    {
        super(factory);
        this.factory = factory;
    }

    protected void init()
    {
        final ActivityManager menuActivityManager = new ActivityManager(factory.getMenuActivityMapper(), factory.getEventBus());
        menuActivityManager.setDisplay(factory.getShell().getMenu());
        
        final ActivityManager contentActivityManager = new ActivityManager(factory.getContentActivityMapper(), factory.getEventBus());
        contentActivityManager.setDisplay(factory.getShell().getContent());
        
        AdminClientPlaceHistoryMapper mapper = GWT.create(AdminClientPlaceHistoryMapper.class);
        PlaceHistoryHandler placeHistoryHandler = new PlaceHistoryHandler(mapper);
        PlaceImpl defaultPlace = AdminMenuPlaceUtil.getBuilderForMenu(AdminMenuItem.CITY).build();
        placeHistoryHandler.register(factory.getPlaceController(), factory.getEventBus(), defaultPlace);
        placeHistoryHandler.handleCurrentHistory();
    }
}
